package com.example.echoewish.ui.my_activity.my_comment_activity;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.example.echoewish.R;
import com.example.echoewish.pojo.MyComment;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.ui.EchoWishCompatActivity;

import java.util.LinkedList;
import java.util.List;

public class MyCommentActivity extends EchoWishCompatActivity {

    private ListView myCommentListView;
    private MyCommentAdapter myCommentAdapter;
    private MyCommentViewModel myCommentViewModel;

    private int nowPosition;

    private Handler myCommentHandler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE:
                    myCommentAdapter.clear();
                    Log.d(Constant.TAG, "handleMessage: 清除成功");
                    myCommentAdapter.addAll(myCommentViewModel.myCommentMutableLiveData.getValue());
                    Log.d(Constant.TAG, "handleMessage: 添加成功");
                    myCommentAdapter.notifyDataSetChanged();
                    break;
                case Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE:
                    Toast.makeText(MyCommentActivity.this,"网络连接出错",Toast.LENGTH_SHORT).show();
                    break;
                case MyCommentViewModel.DELETE_MY_COMMENT_FAIL:
                    Toast.makeText(MyCommentActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
                    break;
                case MyCommentViewModel.DELETE_MY_COMMENT_SUCCESS:
                    Toast.makeText(MyCommentActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                    myCommentAdapter.remove(myCommentViewModel.deleteCommentMutableLiveData.getValue());
                    myCommentAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myCommentAdapter.setButtonClickListener(new MyCommentAdapter.ButtonClickListener() {
            @Override
            public void buttonClickListener(int nowPosition) {
                myCommentViewModel.deleteCommentMutableLiveData.setValue(myCommentAdapter.getItem(nowPosition));
            }
        });
        myCommentViewModel.getCommentByUserId();
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_my_comment);

    }

    public void initView()
    {
        myCommentListView=findViewById(R.id.my_comment_list_view);
        myCommentAdapter=new MyCommentAdapter(this,R.layout.my_comment_item,new LinkedList<MyComment>());
        myCommentListView.setAdapter(myCommentAdapter);
    }

    @Override
    public void initViewModel() {
        myCommentViewModel=new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyCommentViewModel.class);
    }

    @Override
    public void observeLiveData() {
        myCommentViewModel.myCommentMutableLiveData.observe(this, new Observer<List<MyComment>>() {
            @Override
            public void onChanged(List<MyComment> myComments) {
                if(myComments!=null)
                {
                    Message message=new Message();
                    message.what= Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                    myCommentHandler.sendMessage(message);
                }
                else
                {
                    Message message=new Message();
                    message.what= Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE;
                    myCommentHandler.sendMessage(message);
                }
            }
        });

        myCommentViewModel.deleteCommentMutableLiveData.observe(this, new Observer<MyComment>() {
            @Override
            public void onChanged(MyComment comment) {
                myCommentViewModel.deleteComment();
            }
        });

        myCommentViewModel.responseMutableLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Message message=new Message();
                if (s.equals(Constant.HTTPS_GET_CALLBACK_SUCCESS))
                {
                    message.what=MyCommentViewModel.DELETE_MY_COMMENT_SUCCESS;
                    myCommentHandler.sendMessage(message);
                }
                else
                {
                    message.what=MyCommentViewModel.DELETE_MY_COMMENT_FAIL;
                    myCommentHandler.sendMessage(message);
                }
            }
        });
    }

}